<?php
namespace Mobile\Controller;

use Common\Controller\UserBaseController;

/**
 * 用户中心控制器
 */
class UserCenterController extends UserBaseController
{
    /**
     * 首页
     */
    public function index()
    {
        $User = M('Member');
        $user = $User->where('member_id =' . session('member_id'))->find();
        $this->assign('user', $user);
        $this->display('index');
    }

    public function center()
    {
        $User = M('Member');
        $user = $User->where('member_id =' . session('member_id'))->find();
        $this->assign('user', $user);
        $this->display('member-center-index');
    }
    /*
     * 账号设置
     *   */

    public function address()
    {
        $User = M('Member');
        $user = $User->where('member_id =' . session('member_id'))->find();

        $this->assign('user', $user);
        $this->display('member-center-address');

    }

    /*
     * 消息中心
     *   */

    public function notices()
    {
        $User = M('Member');
        $user = $User->where('member_id =' . session('member_id'))->find();

        $Notices = M('Account_log');
        $count   = $Notices->where('m_id=' . session('member_id'))->count();
        $Page    = new \Think\Page($count, 10);
        $Page->setConfig('header', '<li class="rows" style="list-style-type:none;color:#999;">共<b>%TOTAL_ROW%</b>条记录 第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
        $Page->setConfig('prev', '<<上一页');
        $Page->setConfig('next', '<<下一页');
        $Page->setConfig('last', '末页');
        $Page->setConfig('first', '首页');
        $Page->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
        $Page->lastSuffix = false; //最后一页不显示为总页数
        $notices          = $Notices->where('m_id=' . session('member_id'))->order('add_time DESC')->limit($Page->firstRow . ',' . $Page->listRows)->select();

        $map['read'] = 0;
        $map['m_id'] = session('member_id');
        $read        = $Notices->where($map)->count();

        $show = $Page->show();

        $this->assign('user', $user);
        $this->assign('notices', $notices);
        $this->assign('notice', 'notice');
        $this->assign('read', $read);
        $this->assign('page', $show);
        $this->display('member-center-notices');

    }
    /*
     * 点击消息中心改变阅读状态
     * 详细信息
     *   */
    public function shownotice()
    {
        $data = array(
            'read' => 1,

        );
        $id        = I('id');
        $Notices   = M('Account_log');
        $map['id'] = $id;
        $Notices->where($map)->save($data);

        $details = $Notices->where($map)->find();

        $Type = M('Account_change_type');
        $type = $Type->select();

        $this->assign('type', $type);
        $this->assign('details', $details);
        $this->display('details');

    }
    /*
     * ajax查询未读信息总数
     *
     *   */
    public function count_read()
    {
        $Notices     = M('Account_log');
        $map['m_id'] = session('member_id');
        $map['read'] = 0;
        echo $read   = $Notices->where($map)->count();

    }

    /*
     * 意见反馈改变阅读状态
     *详细信息
     *   */
    public function showsuggestions()
    {
        $data = array(
            'status' => 1,

        );
        $id        = I('id');
        $Notices   = M('Suggestions');
        $map['id'] = $id;

        $details              = $Notices->where($map)->find();
        $details['responses'] = htmlspecialchars_decode($details['responses']);
        $details['responses'] = strip_tags($details['responses']);
        $Type                 = M('Suggestions_type');
        $type                 = $Type->select();

        $this->assign('type', $type);
        $this->assign('details', $details);
        $this->display('details2');

    }

    /*
     * 意见反馈
     *   */

    public function feedback()
    {
        $User = M('Member');
        $user = $User->where('member_id =' . session('member_id'))->find();

        $Type        = M('Suggestions_type');
        $Suggestions = M('Suggestions');
        $list        = $Type->where('state=1')->select();
        $type        = $this->ancestry($list, 0);

        $count = $Suggestions->where('uid=' . session('member_id'))->count();
        $Page  = new \Think\Page($count, 5);
        $Page->setConfig('header', '<li class="rows" style="list-style-type:none; color:#999;">共<b>%TOTAL_ROW%</b>条记录 第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('last', '末页');
        $Page->setConfig('first', '首页');
        $Page->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
        $Page->lastSuffix = false; //最后一页不显示为总页数

        $suggestions = $Suggestions->where('uid=' . session('member_id'))->order('createtime DESC')->limit($Page->firstRow . ',' . $Page->listRows)->select();

        $show = $Page->show();

        $this->assign('user', $user);
        $this->assign('type', $type);
        $this->assign('page', $show);
        $this->assign('feedback', 'feedback');
        $this->assign('suggestions', $suggestions);
        $this->display('member-center-feedback');

    }

    /*
     * 获取验证码
     *   */

    public function get_code()
    {
        $code = getRandStr(4);

        session('code', $code);

        echo json_encode(array("success" => 1, "message" => "发送成功，您的验证码是" . $code));

    }

    /*
     * 验证绑定邮箱
     *   */

    public function checkMail()
    {

        if (IS_POST) {
            $type      = I('type');
            $useremail = I('usermail');

        }
        $User                = M('Member');
        $map['member_email'] = $useremail;
        $user                = $User->where($map)->count();
        $code                = getRandStr(4);

        session('code', $code);

        if (!$user) {

            echo json_encode(array("success" => 1, "message" => "发送成功，您的验证码是" . $code));

        } else {

            die(json_encode(array("error" => 1, "message" => "该邮箱地址已被绑定！")));
        }
    }
    /*
     * 绑定邮箱
     *   */

    public function bindMail()
    {

        if (IS_POST) {
            $type      = I('type');
            $useremail = I('usermail');
            $code      = I('code');
            $checkcode = session('code');
            if ($code == $checkcode) {
                $data = array(
                    'member_email'  => $useremail,
                    'is_bind_email' => $type,
                );
                $map['member_id'] = session('member_id');
                $User             = M('Member');
                if ($User->where($map)->save($data)) {
                    echo json_encode(array("success" => 1, "message" => "绑定成功"));
                } else {
                    die(json_encode(array("error" => 0, "message" => "绑定失败")));
                }

            } else {

                die(json_encode(array("error" => 0, "message" => "验证码错误！")));
            }
        }
    }
    /*
     * 验证修改邮箱
     *   */
    public function checkEditMail()
    {
        if (IS_POST) {
            $useremail           = I('usermail');
            $type                = I('type');
            $User                = M('Member');
            $map['member_email'] = $useremail;
            $user                = $User->where($map)->count();
            $code                = getRandStr(4);

            session('code', $code);
            if (!$user) {

                echo json_encode(array("success" => 1, "message" => "发送成功，您的验证码是" . $code));

            } else {

                die(json_encode(array("error" => 1, "message" => "该邮箱已被绑定！")));
            }
        }

    }

    /*
     * 修改邮箱
     *   */
    public function editMail()
    {
        if (IS_POST) {

            $type      = I('type');
            $useremail = I('usermail');
            $code      = I('code');
            $checkcode = session('code');
            if ($code == $checkcode) {
                $data = array(
                    'member_email'  => $useremail,
                    'is_bind_email' => $type,
                );
                $map['member_id'] = session('member_id');
                $User             = M('Member');
                if ($User->where($map)->save($data)) {
                    echo json_encode(array("success" => 1, "message" => "修改成功"));
                } else {
                    die(json_encode(array("error" => 0, "message" => "修改失败")));
                }

            } else {

                die(json_encode(array("error" => 0, "message" => "验证码错误！")));
            }

        }

    }

    /*
     * 验证手机号码是否存在
     *   */
    public function checkMobile()
    {

        if (IS_POST) {

            $type       = I('type');
            $usermobile = I('usermobile');

            $User = M('Member');
            $user = $User->where('member_mobile=' . $usermobile)->count();

            $code = getRandStr(4);

            session('code', $code);
            if ($user) {

                echo json_encode(array("error" => 0, "message" => 手机号码已被绑定！));

            } else {

                echo json_encode(array("success" => 1, "message" => "发送成功，验证码是" . $code));
            }

        }

    }

    /*
     * 绑定手机号码
     *   */

    public function bindMobile()
    {

        if (IS_POST) {
            $type       = I('type');
            $usermobile = I('usermobile');
            $code       = I('code');
            $checkcode  = session('code');
            if ($code == $checkcode) {
                $data = array(
                    'member_mobile'  => $usermobile,
                    'is_bind_mobile' => $type,
                );
                $map['member_id'] = session('member_id');
                $User             = M('Member');
                if ($User->where($map)->save($data)) {
                    echo json_encode(array("success" => 1, "message" => "绑定成功"));
                } else {
                    die(json_encode(array("error" => 0, "message" => "绑定失败")));
                }

            } else {

                die(json_encode(array("error" => 0, "message" => "验证码错误！")));
            }
        }
    }

    /*
     * 验证修改的手机号
     *   */
    public function checkEditMobile()
    {
        if (IS_POST) {
            $usermobile = I('usermobile');
            $type       = I('type');
            $User       = M('Member');
            $user       = $User->where(' member_mobile=' . $usermobile)->count();
            $code       = getRandStr(4);

            session('code', $code);
            if (!$user) {

                echo json_encode(array("success" => 1, "message" => "发送成功，您的验证码是" . $code));

            } else {

                die(json_encode(array("error" => 1, "message" => "该手机号已被绑定！")));
            }
        }

    }

    /*
     * 修改手机
     *   */
    public function editMobile()
    {
        if (IS_POST) {

            $type       = I('type');
            $usermobile = I('usermobile');
            $code       = I('code');
            $checkcode  = session('code');
            if ($code == $checkcode) {
                $data = array(
                    'member_mobile'  => $usermobile,
                    'is_bind_mobile' => $type,
                );
                $map['member_id'] = session('member_id');
                $User             = M('Member');
                if ($User->where($map)->save($data)) {
                    echo json_encode(array("success" => 1, "message" => "修改成功"));
                } else {
                    die(json_encode(array("error" => 0, "message" => "修改失败")));
                }

            } else {

                die(json_encode(array("error" => 0, "message" => "验证码错误！")));
            }

        }

    }

    /*
     * 验证微信是否存在
     *   */
    public function checkWechat()
    {

        if (IS_POST) {

            $wechat        = I('userwechat');
            $User          = M('Member');
            $map['wechat'] = $wechat;
            $user          = $User->where($map)->count();

            if ($user) {

                echo json_encode(array("error" => 0, "message" => "微信号已被绑定"));

            } else {

                echo json_encode(array("success" => 1, "message" => "微信号可以绑定"));
            }

        }

    }

    /*
     * 绑定微信号和修改微信号
     *   */

    public function bindWechat()
    {

        if (IS_POST) {
            $type       = I('type');
            $userwechat = I('userwechat');

            $data = array(
                'wechat' => $userwechat,
            );
            $map['member_id'] = session('member_id');
            $User             = M('Member');
            if ($User->where($map)->save($data)) {

                echo json_encode(array("success" => 1, "message" => "绑定成功"));

            } else {

                die(json_encode(array("error" => 0, "message" => "绑定失败")));

            }

        }
    }

    /*
     * 绑定微信号和修改微信号
     *   */

    public function editWechat()
    {

        if (IS_POST) {
            $type       = I('type');
            $userwechat = I('userwechat');

            $data = array(
                'wechat' => $userwechat,
            );
            $map['member_id'] = session('member_id');
            $User             = M('Member');
            if ($User->where($map)->save($data)) {

                echo json_encode(array("success" => 1, "message" => "修改成功"));

            } else {

                die(json_encode(array("error" => 0, "message" => "修改失败")));

            }

        }
    }
    /*
     * 保存收获地址
     *   */
    public function saveAddress()
    {
        if (IS_POST) {

            $data = array(
                'Consignee'        => I('Consignee'),
                'Consignee_mobile' => I('Consignee_Mobile'),
                'address'          => I('Address'),
            );

            $map['member_id'] = session('member_id');
            $User             = M('Member');
            if ($User->where($map)->save($data)) {
                echo json_encode(array("success" => 1, "message" => "保存成功"));
            } else {
                die(json_encode(array("error" => 0, "message" => "保存失败！")));
            }

        }
    }
    /*
     * 发送意见
     *   */
    public function sendFeedBack()
    {
        if (IS_POST) {

            $data = array(
                'content'    => I('content'),
                'title'      => I('title'),
                'createtime' => time(),
                'uid'        => session('member_id'),
            );
            $Suggestions = M('Suggestions');
            if ($Suggestions->add($data)) {
                echo json_encode(array("success" => 1, "message" => "发送成功"));
            } else {
                die(json_encode(array("error" => 0, "message" => "发送失败")));
            }

        }
    }

    /*
     * 保存用户真实姓名
     *   */
    public function updateUer()
    {
        if (IS_POST) {
            $birthday = I('birthDay');
            $data     = array(
                'true_name' => I('userName'),
                'birthday'  => strtotime($birthday),
                'sex'       => I('sex'),
            );
            $map['member_id'] = session('member_id');
            $User             = M('Member');
            if ($User->where($map)->save($data)) {

                echo json_encode(array("success" => 1, "message" => "绑定成功"));

            } else {

                die(json_encode(array("error" => 0, "message" => "绑定失败")));

            }

        }
    }

    /**
     *  密码修改
     */
    public function password()
    {
        if ($_POST) {
            $password    = $_POST['password'];
            $oldPassword = $_POST['oldPassword'];
            $code        = $_POST['code'];
            if ($code != $_SESSION['code']) {
                die(json_encode(array("error" => 0, "message" => '验证码错误')));
            }
            $pwd = M('member')->where('member_id=' . $_SESSION['member_id'])->getField('member_password');
            if ($pwd != md5($oldPassword)) {
                die(json_encode(array("error" => 0, "message" => '输入了错误的旧密码')));
            }
            $result = M('member')->where('member_id=' . $_SESSION['member_id'])->save(array('member_password' => md5($password)));
            if ($result >= 0) {
                die(json_encode(array("success" => 1, "message" => '密码修改成功')));
            } else {
                die(json_encode(array("error" => 0, "message" => '密码修改失败')));
            }
        }

    }

    //递归分类
    public function ancestry($data, $pid)
    {
        static $ancestry = array();

        foreach ($data as $key => $value) {
            if ($value['parent_id'] == $pid) {
                $ancestry[] = $value;

                $this->ancestry($data, $value['id']);
            }
        }
        return $ancestry;
    }

}
